<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import shopBg from "assets/images/shop-header-bg.png";
    import searchIcon from "assets/images/search-ico-white.png";
    import walletIcon from "assets/images/wallet-icon.png";
    import orderIcon from "assets/images/order-icon.png";
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    export default{
        data(){
            return{
                shopBg: shopBg,
                searchIcon: searchIcon,
                walletIcon: walletIcon,
                orderIcon: orderIcon,

                shopType: null,

                /**限时抢购参数**/
                clockNow: null,
                clockTarget: null,

                catalogList: [ //固定分类有推荐和猜你想要
                    {
                        type: "recommend",
                        name: "推荐"
                    },
                    {
                        type: "guess",
                        name: "猜你想要"
                    }
                ],
                goodsList:[],
                catalogType: { //默认推荐
                    type: "recommend",
                    name: "推荐"
                },

                /**分页参数**/
                start: 0,
                limit: 9,
                loading: false, //正在加载
                listLoaded: false, //默认的页面没有加载
                listCompleted: false//分页完成
            }
        },
        created() {
            let self = this;
            let config = weex.config;
            self.shopType = config.shopType;

            if(!self.shopType) {
                modal.toast("店铺参数有误");
                return false;
            }

            let _options = {
                shoptype: self.shopType,
                start: self.start,
                limit: self.limit
            };
            api.sendHttp("getTypeShopInfoApi",_options).then(res =>{
                self.listLoaded = true;
                self.shopLogo = res.data.logo;
                self.catalogList = self.catalogList.concat(res.data.catalog || []);

                //默认推荐
                let _recommend =  res.data.recommend || {};
                self.clockNow = _recommend.sys_time || null;
                self.clockTarget = _recommend.end_time || null;
                self.goodsList = _recommend.goods || [];
                //总数据量小于limit
                if(self.goodsList.length < self.limit) {
                    self.listCompleted = true;
                }
            },
            res =>{
                modal.toast(res.msg || "获取店铺信息失败");
            });
        },
        methods: {
            sendHttp(succCallback,failCallback){
                let self = this;
                let _options = {
                    shoptype: self.shopType,
                    type: self.catalogType && self.catalogType.type,
                    start: self.start,
                    limit: self.limit
                };

                if(self.catalogType.cateId)  _options.catid = self.catalogType.cateId;

                api.sendHttp("getTypeShopListApi",_options).then(res =>{
                    if(typeof succCallback == "function") succCallback(res)
                },
                res =>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res)
                });
            },
            fetchMore(){
                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                self.sendHttp(res=>{
                    self.loading = false;
                    let _goodsList = self.filterReturnData(res.data);

                    if(_goodsList.length < self.limit) self.listCompleted = true;
                    self.goodsList = self.goodsList.concat(_goodsList);
                },res=>{
                    self.loading = false;
                });
            },
            initData(){
                let self = this;
                self.goodsList.length = 0;
                self.start = 0;
                self.listCompleted = false;
                self.listLoaded = false;
                self.clockNow = null;
                self.clockTarget = null;
            },
            sortClick(catalog){
                let self = this;
                if(self.stringifyObj(self.catalogType) == self.stringifyObj(catalog)) {
                    return false;
                }

                //重置参数
                self.initData();
                self.catalogType = catalog;
                self.sendHttp(res=>{
                    self.listLoaded = true;
                    self.goodsList = self.filterReturnData(res.data);

                    //限时抢购
                    if(self.catalogType.type == 'recommend'){
                        let xsqg = res.data.xsqg || {};
                        self.clockNow = xsqg.sys_time;
                        self.clockTarget = xsqg.end_time;
                    }

                    //总数据量小于limit
                    if(self.goodsList.length < self.limit) {
                        self.listCompleted = true;
                    }
                },res=>{
                     modal.toast(res.msg || "获取店铺商品列表失败");
                });
            },
            filterReturnData(data){
                let self = this;
                if (self.catalogType.type == "recommend"){
                    return data.xsqg.goods || [];
                }
                return data || [];
            },
            stringifyObj(obj){
                obj = obj || {};
                return JSON.stringify(obj);
            },
            alarm(){ //限时抢购刷新
                let self = this;
                if(self.catalogType.type == "recommend") {
                    self.initData();

                    self.sendHttp(res=>{
                        self.listLoaded = true;
                        let xsqg = res.data.xsqg || {};
                        self.clockNow = xsqg.sys_time;
                        self.clockTarget = xsqg.end_time;
                        self.goodsList = self.filterReturnData(xsqg);

                        //总数据量小于limit
                        if(self.goodsList.length < self.limit) {
                            self.listCompleted = true;
                        }
                    },res=>{
                         modal.toast(res.msg);
                    });
                }
            },
            goToSearch(){ //跳转原生搜索页
                let self = this;
                let _options = {
                    classType: constant.Search_Class_Type,
                    title: '搜索',
                    params: {
                        shopType: self.shopType
                    }
                };
                navigator.push(_options);
            },
            goToCoupon(){ //跳转优惠券
                let _options = {
                    url: route.coupon_index,
                    title: '优惠券'
                };
                navigator.push(_options);
            },
            goToOrder(){ //跳转订单
                let _options = {
                    url: route.order_index,
                    title: '我的订单'
                };
                navigator.push(_options);
            }
        }
    }
</script>
